package com.training1.core.web.controller;

import java.io.BufferedOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.training1.core.common.controller.BaseController;
import com.training1.core.common.dao.Page;
import com.training1.core.entity.BatchWithDrawalEntity;
import com.training1.core.modules.service.RemitSucService;
@Controller
@RequestMapping("remitSuc")
public class RemitSucController extends BaseController  {

	@Resource
	private RemitSucService service;
	
	@RequestMapping({ "/", "" })
	public String auditSet() {
		return "yixiang/remitSucSetting";
	}
	
	@RequestMapping("list")
	@ResponseBody
	public Page<BatchWithDrawalEntity> list(String cellphone, String createTimeFrom, String createTimeTo,
			String withdrawCashFrom, String withdrawCashTo,String status, Page<BatchWithDrawalEntity> page) {
		Map<String,Object> map = new HashMap<String,Object>();
		page.getSearch().put("cellphone", cellphone);
		map.put("cellphone", cellphone);
		if (createTimeFrom != null) {
			page.getSearch().put("createTimeFrom", createTimeFrom + " 00:00:00");
			map.put("createTimeFrom", createTimeFrom + " 00:00:00");
		}
		if (createTimeTo != null) {
			page.getSearch().put("createTimeTo", createTimeTo + " 23:59:59");
			map.put("createTimeTo", createTimeTo + " 23:59:59");
		}
			page.getSearch().put("withdrawCashFrom", withdrawCashFrom);
			page.getSearch().put("withdrawCashTo", withdrawCashTo);
			page.getSearch().put("status", status);
			map.put("withdrawCashFrom", withdrawCashFrom);
			map.put("withdrawCashTo", withdrawCashTo);
			map.put("status", status);
		//通过条件查找
		service.findList(page.getSearch());
		//查询金额总和
		
		BatchWithDrawalEntity bde = service.findAllMoney(map);
		if(bde==null){
			bde = new BatchWithDrawalEntity();
		}
		bde.setBankforCard("总合计：");
		List<BatchWithDrawalEntity> cashes = new ArrayList<BatchWithDrawalEntity>();
		cashes.add(bde);
		//设置页面footer,显示总合计
		page.setFooter(cashes);

		return page;
	}
	
	@RequestMapping("/export")
	// @ResponseBody
	public String outExcel(String cellphone, String createTimeFrom, String createTimeTo, String withdrawCashFrom,
			String withdrawCashTo, String status,HttpServletResponse resp) throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("cellphone", cellphone);
		if(createTimeFrom!=null){
			map.put("createTimeFrom", createTimeFrom+" 00:00:00");
		}
		if(createTimeTo!=null){
			map.put("createTimeTo", createTimeTo+" 23:59:59");
		}
		map.put("withdrawCashFrom", withdrawCashFrom);
		map.put("withdrawCashTo", withdrawCashTo);
		map.put("status", status);
		//根据条件查询
		List<BatchWithDrawalEntity> list = service.findList(map);
		service.export(list,resp);
		return null;
	}
}
